#! /bin/sh

set -e

# /etc/init.d/stechec-meta: start and stop the meta_server daemon

DAEMON=/usr/bin/meta_server
META_SERVER_CONFIG_FILE=/etc/stechec.yml
META_SERVER_CHID=stechec:nobody

. /lib/lsb/init-functions

test -x $DAEMON || exit 0

case "$1" in
  start)
        log_daemon_msg "Starting stechec daemon" "meta_server"
        if [ -s /var/run/meta_server.pid ] && kill -0 $(cat /var/run/meta_server.pid) >/dev/null 2>&1; then
                log_progress_msg "apparently already running"
                log_end_msg 0
		exit 0
        fi
        if [ ! -s "$META_SERVER_CONFIG_FILE" ]; then
                log_failure_msg "missing or empty config file $META_SERVER_CONFIG_FILE"
		log_end_msg 1
                exit 1
        fi
        start-stop-daemon --start --quiet --background \
            --pidfile /var/run/meta_server.pid --make-pidfile \
            --nicelevel 5 --chuid $META_SERVER_CHID \
            --exec $DAEMON -- -c $META_SERVER_CONFIG_FILE
        log_end_msg $?
        ;;

  stop)
        log_daemon_msg "Stopping stechec daemon" "meta_server"
        start-stop-daemon --stop --quiet --oknodo \
            --pidfile /var/run/meta_server.pid
        log_end_msg $?
	rm -f /var/run/meta_server.pid
        ;;

  restart)
        $0 stop
        $0 start
        ;;
  *)
	echo "Usage: /etc/init.d/stechec-meta {start|stop|restart}"
	exit 1
        ;;
esac

:
